Dart double operator unary minus
Syntax & Examples


double.operator unary minus operator

The negate operator changes the sign of the given numeric value to its opposite and returns a double result.


Syntax of double.operator unary minus

The syntax of double.operator unary minus operator is:

operator unary-() → double

This operator unary minus operator of double negate operator.



✐ Examples

1 Example with positive value

In this example,

  1. We create a positive numeric value numValue with the value 10.
  2. We then use the negate operator - to change the sign of numValue to its opposite.
  3. We print the negated result to standard output.

Dart Program

void main() {
  num numValue = 10;
  double result = -numValue;
  print('Negate result: $result');
}

Output

Negate result: -10.0

2 Example with floating-point value

In this example,

  1. We create a floating-point value numValue with the value 3.5.
  2. We then use the negate operator - to change the sign of numValue to its opposite.
  3. We print the negated result to standard output.

Dart Program

void main() {
  num numValue = 3.5;
  double result = -numValue;
  print('Negate result: $result');
}

Output

Negate result: -3.5

3 Example with negative value

In this example,

  1. We create a negative numeric value numValue with the value -8.
  2. We then use the negate operator - to change the sign of numValue to its opposite.
  3. We print the negated result to standard output.

Dart Program

void main() {
  num numValue = -8;
  double result = -numValue;
  print('Negate result: $result');
}

Output

Negate result: 8.0

Summary

In this Dart tutorial, we learned about operator unary minus operator of double: the syntax and few working examples with output and detailed explanation for each example.